//
//  MoreQuestionViewController.m
//  XDCommonApp
//
//  Created by maboyi on 14-6-20.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "MoreQuestionViewController.h"
#import "QuestionContentViewController.h"
@interface MoreQuestionViewController ()

@end

@implementation MoreQuestionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    self.titleLabel.text = @"更多问题";
    dataArray = [[NSMutableArray alloc] init];
    contentArray = [[NSMutableArray alloc] init];
//WithObjects:@"什么是喵贷款信用支付?",@"还款的方式有哪些?",@"喵代还款的最长时间是多长?如果逾期不还怎么办?",@"喵贷款如何还款", nil];
    
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT - UI_NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:myTableView];

    [self initData];
    
}

- (void)initData{

    if ([XDTools NetworkReachable])
    {

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://xindaoapp.com/maodai/plugins/API.v1.0/?&a=category&m=new_common"]];
        [request setTimeOutSeconds:12];

        __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{

            [XDTools hideProgress:self.contentView];

            NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];

            if ([[tempDic objectForKey:@"result"] intValue] == 1)
            {
                dataArray = [NSMutableArray arrayWithArray:tempDic[@"data"]];
                [myTableView reloadData];
            }
            else
            {
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
            }
        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
        }];

        [request startAsynchronous];
        [XDTools showProgress:self.contentView];

    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
    
}


#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row == 2) {
//        return 64;
//    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cellIde = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
        
        float h = 49.5f;
//        if (indexPath.row == 2) {
//            h = 63.5f;
//        }
        UIImageView * lineIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, h, UI_SCREEN_WIDTH, .5f)];
        lineIV.image = [UIImage imageNamed:@"line"];
        [cell.contentView addSubview:lineIV];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.text = dataArray[indexPath.row][@"title"];
    cell.textLabel.numberOfLines = 0;
    
    return cell;
}

#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionContentViewController * question = [[QuestionContentViewController alloc] init];
    question.content = dataArray[indexPath.row][@"content"];
    [self.navigationController pushViewController:question animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
